<?php

namespace app\admin\model\booking;

use think\Model;
use think\Db;

class Store extends Model
{

    // 表名
    protected $name = 'booking_store';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];

    protected static function init()
    {

        self::beforeWrite(function ($row) {
            $changeData = $row->getChangedData();
            if (isset($changeData['is_default']) && $changeData['is_default'] == 1) {
                (new \app\admin\model\booking\Store())->where('is_default', 1)->update(['is_default' => 0]);
            }
        });

        self::beforeInsert(function ($row) {
            if ($row['is_default'] != 1) {
                $data = (new \app\admin\model\booking\Store())->where('is_default', 1)->find();
                if (empty($data)) {
                    $row->is_default = 1;
                }
            }
        });

        self::afterInsert(function ($row) {
            $row->save(['weigh' => $row['id']]);

            //新增成功
            $auth = \app\admin\library\Auth::instance();
            $config = get_addon_config('booking');
            //开启授权，切不是超级管理员
            if ($config['isStoreAllocate'] == 1 && !$auth->isSuperAdmin()) {
                $admin_row = \app\admin\model\booking\StoreAdmin::where('admin_id', $auth->id)->find();
                //新增不在，追加
                if ($admin_row) {
                    $admin_row->save(['store_ids' => $admin_row['store_ids'] . ',' . $row['id']]);
                }
            }
        });

        //删除门店，
        self::afterDelete(function ($row) {
            Db::transaction(function () use ($row) {
                $adminStore = \app\admin\model\booking\StoreAdmin::where("FIND_IN_SET('" . $row['id'] . "',store_ids)")->select();
                foreach ($adminStore as $item) {
                    $store_ids = explode(',', $item['store_ids']);
                    $key = array_search($row['id'], $store_ids);
                    unset($store_ids[$key]);
                    $item->save(['store_ids' => join(',', $store_ids)]);
                }
            });
        });
    }

    public function getIsDefaultList()
    {
        return ['0' => __('Is_default 0'), '1' => __('Is_default 1')];
    }

    public function getIsDefaultTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_default']) ? $data['is_default'] : '');
        $list = $this->getToShopPayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
}
